/*
 * Edge.java
 *
 * Created on September 14, 2006, 2:39 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package com.smallx.xproc.compiler.graph;

import org.infoset.xml.Name;
import org.infoset.xml.NamespaceScope;

/**
 *
 * @author alex
 */
public class Edge {
   
   String label;
   boolean directed;
   Vertex source;
   Name outputPort;
   Vertex target;
   Name inputPort;
   String expression;
   NamespaceScope scope;
   
   protected String calculateLabel(Name from,Name to) {
      if (from==null) {
         return "* \u2192 "+to;
      } else if (to==null) {
         return from+" \u2192 *";
      } else {
         return from+" \u2192 "+to;
      }
   }
   
   /** Creates a new instance of Edge */
   public Edge(Vertex source,Name output,Vertex target, Name input) {
      this(source,output,target,input,false);
   }
   
   public Edge(Vertex source,Name output,Vertex target,Name input,boolean directed) {
      this.label = calculateLabel(output,input);
      this.source = source;
      this.outputPort = output;
      this.target = target;
      this.inputPort = input;
      source.add(this);
      this.directed = directed;
      if (!directed) {
         target.add(this);
      }
      this.expression = null;
   }
   
   public String getLabel() {
      return label;
   }
   
   public boolean isDirected() {
      return directed;
   }
   
   public Vertex getSource() {
      return source;
   }
   
   public Vertex getTarget() {
      return target;
   }
   
   public Name getOutputPort() {
      return outputPort;
   }
   
   public Name getInputPort() {
      return inputPort;
   }
   
   public String getExpression() {
      return expression;
   }
   public void setExpression(String value,NamespaceScope scope) {
      this.expression = value;
      this.scope = scope;
      if (expression!=null) {
         this.label += " select="+value;
      }
   }
   public NamespaceScope getNamespaceScope() {
      return scope;
   }
   
}
